﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EFFC.Net.Interface;
using System.Xml.Schema;
using System.Xml;
using EFFC.Net.Common;
using System.IO;

namespace EFFC.Net.Resource.Files
{
    public class XmlControl : IResourceEntity
    {
        private StringBuilder errorstring = new StringBuilder();
        private string path = "";
        private string xsdpath = "";
        XmlDocument xdoc = null;
        string id = "";

        public string FilePath
        {
            set
            {
                this.path = value;
            }

            get
            {
                return this.path;
            }
        }
        public string XSDFilePath
        {
            set
            {
                this.xsdpath = value;
            }

            get
            {
                return this.xsdpath;
            }
        }

        public XmlControl()
        {
            id = "XC" + Guid.NewGuid().ToString();
            xdoc = new XmlDocument();
        }

        public XmlControl(string path)
        {
            id = "XC" + Guid.NewGuid().ToString();
            this.path = path;
            XmlDocument xdoc = new XmlDocument();
        }

        public XmlControl(string path, string xsdpath)
        {
            id = "XC" + Guid.NewGuid().ToString();
            this.path = path;
            this.xsdpath = xsdpath;
            XmlDocument xdoc = new XmlDocument();
        }

        /// <summary>
        /// 根據路徑讀取XML的指定節點
        /// </summary>
        /// <param name="filepath">xml的文件路徑</param>
        /// <param name="xmlpathuri">XPath路徑</param>
        /// <returns></returns>
        public XmlNode ReadNode(string filepath,string xmlpathuri)
        {
            xdoc.Load(filepath);
            return xdoc.SelectSingleNode(xmlpathuri);
        }
        /// <summary>
        /// 根據XML串讀取指定節點
        /// </summary>
        /// <param name="xmlString">XML串</param>
        /// <param name="xmlpathuri">XPath路徑</param>
        /// <returns></returns>
        public XmlNode ReadNode4XMLString(string xmlString, string xmlpathuri)
        {
            xdoc.LoadXml(xmlString);
            return xdoc.SelectSingleNode(xmlpathuri);
        }
        /// <summary>
        /// 根據XML串讀取指定節點列表
        /// </summary>
        /// <param name="xmlString">XML串</param>
        /// <param name="xmlpathuri">XPath路徑</param>
        /// <returns></returns>
        public XmlNodeList ReadNodes4XMLString(string xmlString, string xmlpathuri)
        {
            xdoc.LoadXml(xmlString);
            return xdoc.SelectNodes(xmlpathuri);
        }
        /// <summary>
        /// 根據路徑读取XML的指定多个節點節點
        /// </summary>
        /// <param name="filepath">xml的文件路徑</param>
        /// <param name="xmlpathuri">XPath路徑</param>
        /// <returns></returns>
        public XmlNodeList ReadNodes(string filepath, string xmlpathuri)
        {
            xdoc.Load(filepath);
            return xdoc.SelectNodes(xmlpathuri);
        }
        /// <summary>
        /// 根據路徑读取XML的指定節點
        /// </summary>
        /// <param name="xmlpathuri">XPath路徑</param>
        /// <returns></returns>
        public XmlNode ReadNode(string xmlpathuri)
        {
            return ReadNode(this.path,xmlpathuri);

        }
        /// <summary>
        /// 根據路徑读取XML的指定多个節點
        /// </summary>
        /// <param name="xmlpathuri">XPath路徑</param>
        /// <returns></returns>
        public XmlNodeList ReadNodes(string xmlpathuri)
        {
            return ReadNodes(this.path, xmlpathuri);

        }
        /// <summary>
        ///根據xsd判断xml是否有效
        /// </summary>
        /// <returns></returns>
        public bool IsValid()
        {
            return IsValid(null,this.path, this.xsdpath);
        }
        /// <summary>
        /// 根據XSD判断xml文件是否有效
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public bool IsValid(string ns,string xmlpath, string xsdpath)
        {
            bool rtn = false;
            XmlValidatingReader xmlvReader = null;
            errorstring.Clear();
            try
            {
                XmlSchemaCollection myXmlSchemaCollection = new XmlSchemaCollection();
                myXmlSchemaCollection.Add(ns, xsdpath);
                XmlTextReader xmltRead = new XmlTextReader(xmlpath);
                xmlvReader = new XmlValidatingReader(xmltRead);
                xmlvReader.Schemas.Add(myXmlSchemaCollection);
                xmlvReader.ValidationType = ValidationType.Schema;
                xmlvReader.ValidationEventHandler += new ValidationEventHandler(this.ValidationEventCallBack);   





                //schemaSet = new XmlSchemaSet();
                //sRead = new StringReader(FileControl.LoadFile(xmlpath).ToString());
                //xsdRead = new StringReader(FileControl.LoadFile(xsdpath).ToString());
                //schemaSet.Add(XmlSchema.Read(xsdRead,this.ValidationEventCallBack));

                //XmlReaderSettings settings = new XmlReaderSettings();
                //settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventCallBack);
                //settings.ValidationType = ValidationType.Schema;
                //settings.Schemas = schemaSet;
                //settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;


                //xmlRead = XmlReader.Create(sRead, settings);

                //xsdReader = new XmlTextReader(xsdpath);
                //sc = new XmlSchemaCollection();
                //sc.Add("http://tempuri.org/ResponseFTP.xsd", xsdReader);

                //xtr = new XmlTextReader(xmlpath);

                //xvr = new XmlValidatingReader(xtr);
                //xvr.ValidationEventHandler += new ValidationEventHandler(ValidationEventCallBack);
                //xvr.ValidationType = ValidationType.Schema;
                //xvr.Schemas.Add(sc);

                while (xmlvReader.Read())
                {

                }

                if (errorstring.ToString() == String.Empty)
                {
                    rtn = true;
                }
                else
                {
                    rtn = false;
                }
            }
            finally
            {
                if (xmlvReader != null)
                {
                    xmlvReader.Close();
                }
            }

            return rtn;
        }
        /// <summary>
        /// 调用IsValid校验后，不通过时获得的错误信息
        /// </summary>
        public string ErrorMessage { get { return errorstring.ToString(); } }
        /// <summary>
        /// 根據XSD判断输入xml串是否符合格式要求
        /// </summary>
        /// <param name="inputtext"></param>
        /// <param name="xsdpath"></param>
        /// <returns></returns>
        public bool IsValidForXmlString(string inputtext, string xsdpath)
        {
            return IsValidForXmlString(null, inputtext, xsdpath);
        }

        /// <summary>
        /// 根據XSD判断输入xml串是否符合格式要求
        /// </summary>
        /// <param name="ns"></param>
        /// <param name="inputtext"></param>
        /// <param name="xsdpath"></param>
        /// <returns></returns>
        public bool IsValidForXmlString(string ns,string inputtext, string xsdpath)
        {
            bool rtn = false;
            XmlValidatingReader xmlvReader = null;
            errorstring.Clear();

            try
            {
                XmlSchemaCollection myXmlSchemaCollection = new XmlSchemaCollection();
                myXmlSchemaCollection.Add(ns, xsdpath);
                XmlTextReader xmltRead = new XmlTextReader(new StringReader(inputtext));
                xmlvReader = new XmlValidatingReader(xmltRead);
                xmlvReader.Schemas.Add(myXmlSchemaCollection);
                xmlvReader.ValidationType = ValidationType.Schema;
                xmlvReader.ValidationEventHandler += new ValidationEventHandler(this.ValidationEventCallBack);

                while (xmlvReader.Read())
                {

                }

                if (errorstring.ToString() == String.Empty)
                {
                    rtn = true;
                }
                else
                {
                    rtn = false;
                }
            }
            finally
            {
                if (xmlvReader != null)
                {
                    xmlvReader.Close();
                }
            }

            return rtn;
        }

        private void ValidationEventCallBack(Object sender, ValidationEventArgs e)
        {
            if (e.Severity == XmlSeverityType.Warning)//警告不用处理
            {
            }
            else
            {
                errorstring.AppendLine(e.Message);
            }
        }

        public string ID
        {
            get { return id; }
        }

        public void Release()
        {
            if (xdoc != null)
            {
                xdoc = null;
            }
        }
    }
}
